/**
 * For the license, see main.cpp file
 */

#include "Qrm3Controller.h"
#include "SettingsGlobalResource.h"

#define QRM3_VERSION QString("2.0")

/**
 * Init the null instance for the singletone controller
 */
Qrm3Controller* Qrm3Controller::instance = NULL;

/**
 * Constructor for the controller
 */
Qrm3Controller::Qrm3Controller()
{
	auth = new Authenticator();
	se = new SettingsExpert();
	timer = new CronTimer((quint32)se->getSetting(SETTING_CRON_TIME).toUInt());
	gui = new MainGuiImpl();
	guiUserIdentity = new UserIdentityFormImpl();
	np = new NewsParser();

	/* Time Line */
	timeLine = new QTimeLine(getSettingsExpert()->getSetting(SETTING_CRON_TIME).toUInt() * 1000 * 60, gui->ui.CronProgressBar);
	timeLine->setFrameRange(0, 100);
	timeLine->setLoopCount(0);
	timeLine->setUpdateInterval(1000 * 5);	//update progress bar every 5 seconds (low CPU stress)

	/* Connections */
	connect(timeLine, SIGNAL(frameChanged(int)), gui->ui.CronProgressBar, SLOT(setValue(int)));
	connect(auth, SIGNAL(newLogLine(QString, bool)), gui, SLOT(logLine(QString, bool)));
	connect(np, SIGNAL(newsReady(FeedRSS*)), gui, SLOT(renderNews(FeedRSS*)));
	connect(np, SIGNAL(newsError(QString)), gui, SLOT(renderNewsError(QString)));
	
	/* Is systray available? */
	if (QSystemTrayIcon::isSystemTrayAvailable() && se->getSetting(SETTING_SYSTRAY).toBool())
		gui->isSystemTray = true;
	else
		gui->isSystemTray = false;

	/* Tray icon*/
	if (gui->isSystemTray)
	{
		gui->createActions();
		gui->createTrayIcon();
	}

	/* Connections */
	gui->setConnections();

	/* load settings stored into the gui */
	loadSettingsOnGui();

	gui->logLine("QRm3WiFi " + QRM3_VERSION + " - By Slux");
	gui->setWindowTitle("QRm3WiFi " + QRM3_VERSION);
	gui->ui.version->setText(gui->ui.version->text() + " " + QRM3_VERSION);
	gui->fillRSSList(np->getRssUrls()->keys());
}

/**
 * show the GUI
 */
void Qrm3Controller::showGui()
{
	gui->show();
}

/**
 * show the GUI user identity
 */
void Qrm3Controller::showGuiUserIdentity()
{
	guiUserIdentity->show();
}

/**
 * Fill the settings inside the GUI
 */
void Qrm3Controller::loadSettingsOnGui()
{
	/* Settings gui */
	gui->ui.usernameLineEdit->setText(se->getSetting(SETTING_USERNAME).toString());
	gui->ui.passwordLineEdit->setText
		(
			QString
				(
					QByteArray::fromBase64(se->getSetting(SETTING_PASSWORD).toByteArray())
				)
		);
	gui->ui.serverAddressLineEdit->setText(se->getSetting(SETTING_SERVER_IP).toString());
	gui->ui.serverPortSpinBox->setValue(se->getSetting(SETTING_SERVER_PORT).toUInt());
	gui->ui.cronTimeSpinBox->setValue(se->getSetting(SETTING_CRON_TIME).toUInt());

	if (se->getSetting(SETTING_SYSTRAY).toBool())
		gui->ui.trayIconCheckBox->setCheckState(Qt::Checked);
	else
		gui->ui.trayIconCheckBox->setCheckState(Qt::Unchecked);
	
	gui->ui.nickNameLabel->setText(se->getSetting(SETTINGS_CHAT_NICKNAME).toString());
	
	/* User Identity GUI */
	guiUserIdentity->ui.nickName->setText(se->getSetting(SETTINGS_CHAT_NICKNAME).toString());
	guiUserIdentity->ui.realName->setText(se->getSetting(SETTINGS_CHAT_REALNAME).toString());
}

/**
 * Singletone get instance
 */
Qrm3Controller* Qrm3Controller::getInstance()
{
	if (instance == NULL)
	{
		instance = new Qrm3Controller();
	}

	return instance;
}

/**
 * Gets methods
 */
SettingsExpert* Qrm3Controller::getSettingsExpert()
{
	return se;
}

CronTimer* Qrm3Controller::getCronTimer()
{
	return timer;
}

QTimeLine* Qrm3Controller::getTimeLine()
{
	return timeLine;
}

Authenticator* Qrm3Controller::getAuthenticator()
{
	return auth;
}

MainGuiImpl* Qrm3Controller::getGui()
{
	return gui;
}

UserIdentityFormImpl* Qrm3Controller::getUserIdentityGui()
{
	return guiUserIdentity;
}

NewsParser* Qrm3Controller::getNewsParser()
{
	return np;
}

/**
 * Unlock the chat widgets
 */
void Qrm3Controller::unlockChat()
{
	gui->ui.chatMessage->setEnabled(true);
	gui->ui.lockUnlockButton->setEnabled(true);
}

/**
 * Lock the chat widgets
 */
void Qrm3Controller::lockChat()
{
	gui->ui.chatMessage->setEnabled(false);
	gui->ui.lockUnlockButton->setEnabled(false);
}

